/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.grid.enhanced.dnd._DndMovingManager"]){
dojo._hasResource["dojox.grid.enhanced.dnd._DndMovingManager"]=true;
dojo.provide("dojox.grid.enhanced.dnd._DndMovingManager");
dojo.require("dojox.grid.enhanced.dnd._DndSelectingManager");
dojo.require("dojox.grid.enhanced.dnd._DndMover");
dojo.require("dojo.dnd.move");
dojo.declare("dojox.grid.enhanced.dnd._DndMovingManager",dojox.grid.enhanced.dnd._DndSelectingManager,{exceptRowsTo:-1,exceptColumnsTo:-1,coverDIVs:[],movers:[],constructor:function(_1){
if(this.grid.indirectSelection){
this.exceptColumnsTo=this.grid.pluginMgr.getFixedCellNumber()-1;
}
this.coverDIVs=this.movers=[];
dojo.subscribe("CTRL_KEY_DOWN",dojo.hitch(this,function(_2,_3){
if(_2==this.grid&&_2!=this){
this.keyboardMove(_3);
}
}));
dojo.forEach(this.grid.views.views,function(_4){
dojo.connect(_4.scrollboxNode,"onscroll",dojo.hitch(this,function(){
this.clearDrugDivs();
}));
},this);
},getGridWidth:function(){
return dojo.coords(this.grid.domNode).w-this.grid.views.views[0].getWidth().replace("px","");
},isColSelected:function(_5){
return this.selectedColumns[_5]&&_5>this.exceptColumnsTo;
},getHScrollBarHeight:function(){
this.scrollbarHeight=0;
dojo.forEach(this.grid.views.views,function(_6,_7){
if(_6.scrollboxNode){
var _8=_6.scrollboxNode.offsetHeight-_6.scrollboxNode.clientHeight;
this.scrollbarHeight=_8>this.scrollbarHeight?_8:this.scrollbarHeight;
}
},this);
return this.scrollbarHeight;
},getExceptionalColOffsetWidth:function(){
if(!this.grid.indirectSelection||!this.grid.rowSelectCell){
return 0;
}
var _9=(normalizedOffsetWidth=0),_a=this.grid.rowSelectCell.view.scrollboxNode;
dojo.forEach(this.getHeaderNodes(),function(_b,_c){
if(_c<=this.exceptColumnsTo){
var _d=dojo.coords(_b);
_9+=_d.w;
}
},this);
normalizedOffsetWidth=_9-_a.scrollLeft*(dojo._isBodyLtr()?1:(dojo.isMoz?-1:1));
return normalizedOffsetWidth>0?normalizedOffsetWidth:0;
},getGridCoords:function(){
if(!this.gridCoords){
this.gridCoords=new Object();
if(!this.headerHeight){
this.headerHeight=dojo.coords(this.getHeaderNodes()[0]).h;
}
var _e=dojo.coords(this.grid.views.views[0].domNode);
var _f=dojo.coords(this.grid.domNode);
this.gridCoords.h=_f.h-this.headerHeight-this.getHScrollBarHeight();
this.gridCoords.t=_f.y;
this.gridCoords.l=_f.x+_e.w;
this.gridCoords.w=_f.w-_e.w;
}
return this.gridCoords;
},createAvatar:function(_10,_11,_12,top,_13){
this.gridCoords=null;
var _14=this.getGridCoords();
var _15=dojo.doc.createElement("DIV");
_15.className="dojoxGridSelectedDIV";
_15.id="grid_dnd_cover_div_"+_12+"_"+top;
_15.style.width=_10+"px";
var _16=dojo._docScroll();
var _17=top<_14.t+this.headerHeight?_14.t+this.headerHeight-top:0;
var _18=_14.t+_14.h+this.headerHeight;
var _19=0;
if(top<_14.t+this.headerHeight){
_19=(_14.t+this.headerHeight);
}else{
if(top>_18){
_19=10000;
}else{
_19=top;
}
}
_15.style.top=_19+_16.y+"px";
_15.style.left=(_12+_16.x)+"px";
var _1a=_19+_11-_17;
if(_1a>_18+(_13?this.scrollbarHeight:0)){
_1a=_18;
}
_15.style.height=((_1a-_19)>=0?(_1a-_19):0)+"px";
dojo.doc.body.appendChild(_15);
_15.connections=[];
_15.connections.push(dojo.connect(_15,"onmouseout",this,function(){
this.clearDrugDivs();
}));
_15.connections.push(dojo.connect(_15,"onclick",this,"avataDivClick"));
_15.connections.push(dojo.connect(_15,"keydown",this,function(e){
this.handleESC(e,this);
}));
this.coverDIVs.push(_15);
return _15;
},handleESC:function(e,_1b){
var dk=dojo.keys;
switch(e.keyCode){
case dk.ESCAPE:
try{
this.cancelDND();
}
catch(e){
}
break;
}
},cancelDND:function(){
this.cleanAll();
this.clearDrugDivs();
if(this.mover){
this.mover.destroy();
}
this.cleanAll();
},createCoverMover:function(_1c,_1d,_1e,top,_1f){
var _20=this.getGridCoords(),_21=(_1f=="col"?true:false);
var box={box:{l:(_1f=="row"?_1e:_20.l)+dojo._docScroll().x,t:(_1f=="col"?top:_20.t)+dojo._docScroll().y,w:_1f=="row"?1:_20.w,h:_1f=="col"?1:_20.h+this.headerHeight},mover:dojox.grid.enhanced.dnd._DndMover};
return new dojo.dnd.move.boxConstrainedMoveable(this.createAvatar(_1c,_1d,_1e,top,_21),box);
},getBorderDiv:function(){
var _22=dojo.byId("borderDIV"+this.grid.id);
if(_22==null){
_22=dojo.doc.createElement("DIV");
_22.id="borderDIV"+this.grid.id;
_22.className="dojoxGridBorderDIV";
dojo.doc.body.appendChild(_22);
}
return _22;
},setBorderDiv:function(_23,_24,_25,top){
var _26=this.getBorderDiv();
dojo.style(_26,{"height":_24+"px","top":top+"px","width":_23+"px","left":_25+"px"});
return _26;
},removeOtherMovers:function(id){
if(!this.coverDIVs.hasRemovedOtherMovers){
var _27;
dojo.forEach(this.coverDIVs,function(div){
if(div.id!=id){
dojo.doc.body.removeChild(div);
}else{
_27=div;
}
},this);
this.coverDIVs=[_27];
this.coverDIVs.hasRemovedOtherMovers=true;
}
},addColMovers:function(){
var _28=-1;
dojo.forEach(this.selectedColumns,function(col,_29){
if(this.isColSelected(_29)){
if(_28==-1){
_28=_29;
}
if(this.selectedColumns[_29+1]==null){
this.addColMover(_28,_29);
_28=-1;
}
}
},this);
},addColMover:function(_2a,_2b){
if(this.lock){
return;
}
var _2c=(_2d=0);
var top=null,_2e=null;
if(dojo._isBodyLtr()){
dojo.forEach(this.getHeaderNodes(),function(_2f,_30){
var _31=dojo.coords(_2f);
if(_30==_2a){
_2c=_31.x;
top=_31.y+_31.h;
_2e=_31.h;
}
if(_30==_2b){
_2d=_31.x+_31.w;
}
});
}else{
dojo.forEach(this.getHeaderNodes(),function(_32,_33){
var _34=dojo.coords(_32);
if(_33==_2a){
_2d=_34.x+_34.w;
_2e=_34.h;
}
if(_33==_2b){
_2c=_34.x;
top=_34.y+_34.h;
}
});
}
var _35=this.normalizeColMoverCoords(_2c,_2d,_2a,_2b);
var _36=_35.h,_37=_35.w,_2c=_35.l,_2d=_35.r;
var _38=this.createCoverMover(_37,_36,_2c,top,"col");
this.movers.push(_38);
var _39=this.setBorderDiv(3,_36,-1000,top+dojo._docScroll().y);
dojo.attr(_39,"colH",_35.colH);
dojo.connect(_38,"onMoveStart",dojo.hitch(this,function(_3a,_3b){
this.mover=_3a;
this.removeOtherMovers(_3a.node.id);
}));
dojo.connect(_38,"onMove",dojo.hitch(this,function(_3c,_3d,_3e){
if(_3c.node==null||_3c.node.parentNode==null){
return;
}
this.isMoving=true;
this.moveColBorder(_3c,_3e,_39);
}));
dojo.connect(_38,"onMoveStop",dojo.hitch(this,function(_3f){
this.isMoving=false;
this.mover=null;
if(this.drugDestIndex==null||this.isContinuousSelection(this.selectedColumns)&&(this.drugDestIndex==_2a||this.drugDestIndex==_2b||this.drugDestIndex==(_2b+1)&&this.drugBefore)){
this.movingIgnored=true;
return;
}
this.startMoveCols();
this.drugDestIndex=null;
}));
},normalizeColMoverCoords:function(_40,_41,_42,_43){
var _44=_41-_40,_45=this.grid.views.views,_46=this.grid.pluginMgr;
var _47={"w":_44,"h":0,"l":_40,"r":_41,"colH":0};
var _48=this.getGridWidth()-_45[_45.length-1].getScrollbarWidth();
var rtl=!dojo._isBodyLtr();
var _49=_46.getViewByCellIdx(!rtl?_42:_43);
var _4a=_46.getViewByCellIdx(!rtl?_43:_42);
var _4b=(_49==_4a);
if(!_49||!_4a){
return _47;
}
var _4c=dojo.coords(_49.scrollboxNode).x+(rtl&&dojo.isIE?_49.getScrollbarWidth():0);
var _4d=dojo.coords(_4a.scrollboxNode);
var _4e=_4d.x+_4d.w-((!rtl||!dojo.isIE)?_4a.getScrollbarWidth():0);
if(_47.l<_4c){
_47.w=_47.r-_4c;
_47.l=_4c;
}
if(_47.r>_4e){
_47.w=_4e-_47.l;
}
var i,_4f=this.grid.views.views[0],_50=dojo.coords(_4f.contentNode).h;
var _51=_4a,_52=_4d.h;
_47.colH=_50;
_52=!_4b?_52:(_52-(_51.scrollboxNode.offsetHeight-_51.scrollboxNode.clientHeight));
_47.h=_50<_52?_50:_52;
return _47;
},moveColBorder:function(_53,_54,_55){
var _56=dojo._docScroll(),rtl=!dojo._isBodyLtr();
_54.x-=_56.x;
dojo.forEach(this.getHeaderNodes(),dojo.hitch(this,function(_57,_58){
if(_58>this.exceptColumnsTo){
var _59=dojo.coords(_57);
if(_54.x>=_59.x&&_54.x<=_59.x+_59.w){
if(!this.selectedColumns[_58]||!this.selectedColumns[_58-1]){
_55.style.left=(_59.x+_56.x+(rtl?_59.w:0))+"px";
this.drugDestIndex=_58;
this.drugBefore=true;
!dojo.isIE&&this.normalizeColBorderHeight(_55,_58);
}
}else{
if(this.getHeaderNodes()[_58+1]==null&&(!rtl?(_54.x>_59.x+_59.w):(_54.x<_59.x))){
_55.style.left=(_59.x+_56.x+(rtl?0:_59.w))+"px";
this.drugDestIndex=_58;
this.drugBefore=false;
!dojo.isIE&&this.normalizeColBorderHeight(_55,_58);
}
}
}
}));
},normalizeColBorderHeight:function(_5a,_5b){
var _5c=this.grid.pluginMgr.getViewByCellIdx(_5b);
if(!_5c){
return;
}
var _5d=_5c.scrollboxNode,_5e=dojo.attr(_5a,"colH");
var _5f=dojo.coords(_5d).h-(_5d.offsetHeight-_5d.clientHeight);
_5f=_5e>0&&_5e<_5f?_5e:_5f;
dojo.style(_5a,"height",_5f+"px");
},avataDivClick:function(e){
if(this.movingIgnored){
this.movingIgnored=false;
return;
}
this.cleanAll();
this.clearDrugDivs();
},startMoveCols:function(){
this.changeCursorState("wait");
this.srcIndexdelta=0;
deltaColAmount=0;
dojo.forEach(this.selectedColumns,dojo.hitch(this,function(col,_60){
if(this.isColSelected(_60)){
if(this.drugDestIndex>_60){
_60-=deltaColAmount;
}
deltaColAmount+=1;
var _61=this.grid.layout.cells[_60].view.idx;
var _62=this.grid.layout.cells[this.drugDestIndex].view.idx;
if(_60!=this.drugDestIndex){
this.grid.layout.moveColumn(_61,_62,_60,this.drugDestIndex,this.drugBefore);
}
if(this.drugDestIndex<=_60&&this.drugDestIndex+1<this.grid.layout.cells.length){
this.drugDestIndex+=1;
}
}
}));
var _63=this.drugDestIndex+(this.drugBefore?0:1);
this.clearDrugDivs();
this.cleanAll();
this.resetCellIdx();
this.drugSelectionStart.colIndex=_63-deltaColAmount;
this.drugSelectColumn(this.drugSelectionStart.colIndex+deltaColAmount-1);
},changeCursorState:function(_64){
dojo.forEach(this.coverDIVs,function(div){
div.style.cursor="wait";
});
},addRowMovers:function(){
var _65=-1;
dojo.forEach(this.grid.selection.selected,function(row,_66){
var _67=this.grid.views.views[0];
if(row&&_67.rowNodes[_66]){
if(_65==-1){
_65=_66;
}
if(this.grid.selection.selected[_66+1]==null||!_67.rowNodes[_66+1]){
this.addRowMover(_65,_66);
_65=-1;
}
}
},this);
},addRowMover:function(_68,to){
var _69=0,_6a=this.grid.views.views;
dojo.forEach(_6a,function(_6b,_6c){
_69+=_6b.getScrollbarWidth();
});
var _6d=_6a[_6a.length-1].getScrollbarWidth();
var _6e=!dojo._isBodyLtr()?(dojo.isIE?_69-_6d:_69):0;
var _6f=this.getGridWidth()-_6d;
var _70=this.grid.views.views[0];
var _71=_70.rowNodes[_68],_72=_70.rowNodes[to];
if(!_71||!_72){
return;
}
var _73=dojo.coords(_71),_74=dojo.coords(_72);
var _75=this.getExceptionalColOffsetWidth();
var _76=this.createCoverMover(_6f-_75,(_74.y-_73.y+_74.h),dojo._isBodyLtr()?(_73.x+_73.w+_75):(_73.x-_6f-_6e),_73.y,"row");
var _77=this.setBorderDiv(_6f,3,(dojo._isBodyLtr()?(_74.x+_74.w):(_74.x-_6f-_6e))+dojo._docScroll().x,-100);
var _78=dojo.connect(_76,"onMoveStart",dojo.hitch(this,function(_79,_7a){
this.mover=_79;
this.removeOtherMovers(_79.node.id);
}));
var _7b=dojo.connect(_76,"onMove",dojo.hitch(this,function(_7c,_7d,_7e){
if(_7c.node==null||_7c.node.parentNode==null){
return;
}
this.isMoving=true;
this.moveRowBorder(_7c,_7d,_77,_7e);
}));
var _7f=dojo.connect(_76,"onMoveStop",dojo.hitch(this,function(_80){
this.isMoving=false;
this.mover=null;
this.grid.select.outRangeY=false;
this.grid.select.moveOutTop=false;
if(this.avaOnRowIndex==null||this.isContinuousSelection(this.grid.selection.selected)&&(this.avaOnRowIndex==_68||this.avaOnRowIndex==(to+1))){
this.movingIgnored=true;
return;
}
this.grid.scroller.findScrollTop(this.grid.scroller.page*this.grid.scroller.rowsPerPage);
this.startMoveRows();
this.avaOnRowIndex=null;
delete _76;
}));
},moveRowBorder:function(_81,_82,_83,_84){
var _85=this.getGridCoords(),_86=dojo._docScroll();
_82.t-=_86.y;
_84.y-=_86.y;
if(_82.t-dojo.coords(this.grid.domNode).y-this.grid.domNode.offsetHeight>0){
if(!this.grid.select.outRangeY&&_84.y>=_85.t+_85.h){
this.grid.select.outRangeY=true;
this.autoMoveToNextRow();
}
}else{
if(_84.y<=_85.t+this.headerHeight){
if(!this.grid.select.moveOutTop){
this.grid.select.moveOutTop=true;
this.autoMoveToPreRow();
}
}else{
this.grid.select.outRangeY=false;
this.grid.select.moveOutTop=false;
var _87=this.grid.views.views[0],_88=this.getViewRowNodes(_87.rowNodes);
var _89=dojo.coords(_87.contentNode).h;
var _8a=dojo.coords(_88[_88.length-1]);
if(_89<_85.h&&_84.y>(_8a.y+_8a.h)){
this.avaOnRowIndex=_88.length;
dojo.style(_83,{"top":_8a.y+_8a.h+_86.y+"px"});
return;
}
var _8b;
dojo.forEach(_88,function(_8c,_8d){
if(!_8c||isNaN(parseInt(_8d))){
return;
}
_8b=dojo.coords(_8c);
if(_84.y>_8b.y&&_84.y<_8b.y+_8b.h){
if(!this.grid.selection.selected[_8d]||!this.grid.selection.selected[_8d-1]){
this.avaOnRowIndex=_8d;
dojo.style(_83,{"top":_8b.y+_86.y+"px"});
}
}
},this);
}
}
},autoMoveToPreRow:function(){
if(this.grid.select.moveOutTop){
if(this.grid.scroller.firstVisibleRow>0){
this.grid.scrollToRow(this.grid.scroller.firstVisibleRow-1);
this.autoMoveBorderDivPre();
setTimeout(dojo.hitch(this,"autoMoveToPreRow"),this.autoScrollRate);
}
}
},autoMoveBorderDivPre:function(){
this.avaOnRowIndex--;
var _8e=this.getBorderDiv();
_8e.style.top=dojo.coords(this.grid.views.views[0].rowNodes[this.avaOnRowIndex]).y+dojo._docScroll().y+"px";
},autoMoveToNextRow:function(){
if(this.grid.select.outRangeY){
this.grid.scrollToRow(this.grid.scroller.firstVisibleRow+1);
this.autoMoveBorderDiv();
setTimeout(dojo.hitch(this,"autoMoveToNextRow"),this.autoScrollRate);
}
},autoMoveBorderDiv:function(){
this.avaOnRowIndex++;
var _8f=this.getBorderDiv();
_8f.style.top=dojo.coords(this.grid.views.views[0].rowNodes[this.avaOnRowIndex]).y+dojo._docScroll().y+"px";
},startMoveRows:function(){
var _90=-1;
var _91=0;
dojo.forEach(this.grid.selection.selected,function(row,_92){
if(row){
if(_90==-1){
_90=_92;
}
if(this.grid.selection.selected[_92+1]==null){
_91=this.moveRows(_90,_92,_91);
_90=-1;
}
}
},this);
try{
this.clearDrugDivs();
this.cleanAll();
this.drugSelectionStart.rowIndex=this.avaOnRowIndex-_91;
this.drugSelectRow(this.drugSelectionStart.rowIndex+_91-1);
this.publishRowMove();
}
catch(e){
}
},moveRows:function(_93,end,_94){
if(this.avaOnRowIndex>end){
_93-=_94;
end-=_94;
}
var _95=end-_93+1;
_94+=_95;
var _96=[];
for(var i=0;i<_95;i++){
_96[i]=this.grid._by_idx[_93+i];
this.grid._by_idx[_93+i]=this.grid._by_idx[_93+i+_95];
}
if(this.avaOnRowIndex>end){
for(i=end+1;i<this.avaOnRowIndex-_95;i++){
this.grid._by_idx[i]=this.grid._by_idx[i+_95];
}
var _97=this.avaOnRowIndex-_95;
for(i=_97;i<this.avaOnRowIndex;i++){
this.grid._by_idx[i]=_96[i-_97];
}
for(i=_93;i<this.avaOnRowIndex;i++){
this.grid.updateRow(i);
}
}else{
if(this.avaOnRowIndex<end){
for(i=end;i>this.avaOnRowIndex+_95-1;i--){
this.grid._by_idx[i]=this.grid._by_idx[i-_95];
}
for(i=this.avaOnRowIndex;i<this.avaOnRowIndex+_95;i++){
this.grid._by_idx[i]=_96[i-this.avaOnRowIndex];
}
for(i=this.avaOnRowIndex;i<=end;i++){
this.grid.updateRow(i);
}
}
}
if(this.avaOnRowIndex<=_93){
this.avaOnRowIndex+=_95;
}
return _94;
},clearDrugDivs:function(){
if(!this.isMoving){
var _98=this.getBorderDiv();
_98.style.top=-100+"px";
_98.style.height="0px";
_98.style.left=-100+"px";
dojo.forEach(this.coverDIVs,function(div){
dojo.forEach(div.connections,function(_99){
dojo.disconnect(_99);
});
dojo.doc.body.removeChild(div);
delete div;
},this);
this.coverDIVs=[];
}
},setDrugCoverDivs:function(_9a,_9b){
if(!this.isMoving){
if(this.isColSelected(_9a)){
this.addColMovers();
}else{
if(this.grid.selection.selected[_9b]){
this.addRowMovers();
}else{
this.clearDrugDivs();
}
}
}
},resetCellIdx:function(){
var _9c=0;
var _9d=-1;
dojo.forEach(this.grid.views.views,function(_9e,_9f){
if(_9f==0){
return;
}
if(_9e.structure.cells&&_9e.structure.cells[0]){
dojo.forEach(_9e.structure.cells[0],function(_a0,_a1){
var _a2=_a0.markup[2].split(" ");
var idx=_9c+_a1;
_a2[1]="idx=\""+idx+"\"";
_a0.markup[2]=_a2.join(" ");
});
}
for(i in _9e.rowNodes){
if(!_9e.rowNodes[i]){
return;
}
dojo.forEach(_9e.rowNodes[i].firstChild.rows[0].cells,function(_a3,_a4){
if(_a3&&_a3.attributes){
if(_a4+_9c>_9d){
_9d=_a4+_9c;
}
var idx=document.createAttribute("idx");
idx.value=_a4+_9c;
_a3.attributes.setNamedItem(idx);
}
});
}
_9c=_9d+1;
});
},publishRowMove:function(){
dojo.publish(this.grid.rowMovedTopic,[this]);
},keyboardMove:function(_a5){
var _a6=this.selectedColumns.length>0;
var _a7=dojo.hitch(this.grid.selection,dojox.grid.Selection.prototype["getFirstSelected"])()>=0;
var i,_a8,dk=dojo.keys,_a9=_a5.keyCode;
if(!dojo._isBodyLtr()){
_a9=(_a5.keyCode==dk.LEFT_ARROW)?dk.RIGHT_ARROW:(_a5.keyCode==dk.RIGHT_ARROW?dk.LEFT_ARROW:_a9);
}
switch(_a9){
case dk.LEFT_ARROW:
if(!_a6){
return;
}
_a8=this.getHeaderNodes().length;
for(i=0;i<_a8;i++){
if(this.isColSelected(i)){
this.drugDestIndex=i-1;
this.drugBefore=true;
break;
}
}
var _aa=this.grid.indirectSelection?1:0;
(this.drugDestIndex>=_aa)?this.startMoveCols():(this.drugDestIndex=_aa);
break;
case dk.RIGHT_ARROW:
if(!_a6){
return;
}
_a8=this.getHeaderNodes().length;
this.drugBefore=true;
for(i=0;i<_a8;i++){
if(this.isColSelected(i)&&!this.isColSelected(i+1)){
this.drugDestIndex=i+2;
if(this.drugDestIndex==_a8){
this.drugDestIndex--;
this.drugBefore=false;
}
break;
}
}
if(this.drugDestIndex<_a8){
this.startMoveCols();
}
break;
case dk.UP_ARROW:
if(!_a7){
return;
}
this.avaOnRowIndex=dojo.hitch(this.grid.selection,dojox.grid.Selection.prototype["getFirstSelected"])()-1;
if(this.avaOnRowIndex>-1){
this.startMoveRows();
}
break;
case dk.DOWN_ARROW:
if(!_a7){
return;
}
for(i=0;i<this.grid.rowCount;i++){
if(this.grid.selection.selected[i]&&!this.grid.selection.selected[i+1]){
this.avaOnRowIndex=i+2;
break;
}
}
if(this.avaOnRowIndex<=this.grid.rowCount){
this.startMoveRows();
}
}
}});
}
